import 'package:flutter/material.dart';
import 'package:flutter_ahj_app/base/utils/clickable.dart';
import 'package:flutter_ahj_app/base/values/SQColor.dart';
import 'package:flutter_ahj_app/base/values/dimens.dart';

typedef void ClickCallback();

class MTopBar {
  Widget child; //child
  String title; //标题文本
  String leftText; //左侧文字
  bool centerTitle = false; //是否居中显示title，默认否
  bool hideleftIcon = false; //是否隐藏左侧图标，默认不隐藏
  bool hideleftIcon1 = false; //是否隐藏左侧图标，默认不隐藏
  IconData leftButtonIcon; //左侧图标
  Color leftButtonColor = SQColor.white; //左侧图标颜色
  ClickCallback onLeftButtonIconClick; //左侧图标点击的回调事件
  IconData rightButtonIcon; //右侧图标
  Color rightButtonColor = SQColor.theme_color; //右侧图标颜色
  ClickCallback onRightButtonIconClick; //右侧图标点击事件
  String rightButtonText; //右侧文本
  ClickCallback onRightButtonClick; //右侧文本点击事件
  Color backgroundColor; //背景颜色
  Color titleTextColor = SQColor.white;
  Color rightButtonTextColor = SQColor.white;
  PreferredSizeWidget bottom;
  double elevation; //海拔
  List<Widget> actions;
  Widget leading;

  Color iconColor;
  Brightness brightness;

  //以下为AppBar的部分属性，如后期有需要可以继续添加
  /*  Widget flexibleSpace;
  ShapeBorder shape;
  Brightness brightness;
  IconThemeData iconTheme;
  IconThemeData actionsIconTheme;
  TextTheme textTheme;
  bool primary;
  bool centerTitle;
  double titleSpacing;
  double toolbarOpacity;
  double bottomOpacity;*/
  MTopBar({
    Key key,
    this.child,
    this.title,
    this.leftText,
    this.centerTitle = false,
    this.hideleftIcon = false,
    this.hideleftIcon1 = false,
    this.leftButtonIcon,
    this.leftButtonColor,
    this.onLeftButtonIconClick,
    this.rightButtonIcon,
    this.rightButtonColor,
    this.onRightButtonIconClick,
    this.rightButtonText,
    this.rightButtonTextColor,
    this.onRightButtonClick,
    this.backgroundColor,
    this.titleTextColor,
    this.bottom,
    this.elevation,
    this.actions,
    this.iconColor,
    this.brightness,
    this.leading,
  });

  PreferredSizeWidget create() {
    return _appBar();
  }

  _appBar() {
    if (child != null) {
      return AppBar(
          title: child,
          automaticallyImplyLeading: hideleftIcon1,
          centerTitle: true,
          actions: actions == null ? _actions() : actions,
          brightness: brightness);
    }

    return AppBar(
        title: Text(
          getStr(title),
          style: new TextStyle(color: titleTextColor),
        ),
        centerTitle: true,
        automaticallyImplyLeading: !hideleftIcon,
        bottom: bottom,
        elevation: elevation,
        backgroundColor: backgroundColor == null ? SQColor.theme_color : backgroundColor,
        leading: leading != null ? leading : leftText != null
            ? Container(
                margin: EdgeInsets.only(left: 16),
                alignment: Alignment.center,
                child: Text(
                  leftText,
                  style: TextStyle(
                      color: SQColor.getColor("#212426"),
                      fontSize: 18,
                      fontWeight: FontWeight.w700),
                ),
              )
            : _leftIcon(),
        actions: actions == null ? _actions() : actions,
        iconTheme: iconColor == null ? null : IconThemeData(color: iconColor),
        brightness: brightness);
  }

  _leftIcon() {
    if (onLeftButtonIconClick == null) {
      if (leftButtonIcon == null) {
        return null;
      } else {
//        return Icon(leftButtonIcon);
        return Builder(
          builder: (BuildContext context) {
            return IconButton(
                icon: Icon(
                  leftButtonIcon,
                  color:
                      leftButtonColor == null ? SQColor.white : leftButtonColor,
                ),
                onPressed: () {
                  Navigator.pop(context);
                });
          },
        );
      }
    } else {
      if (leftButtonIcon == null) {
        return Text("");
      } else {
        return IconButton(
            icon: Icon(
              leftButtonIcon,
              color: leftButtonColor == null ? SQColor.white : leftButtonColor,
            ),
            onPressed: onLeftButtonIconClick);
      }
    }
  }

  _actions() {
    List<Widget> widgets = List();
    Widget rightText = _rightText();
    Widget rightIcon = _rightIcon();
    if (rightText != null) widgets.add(rightText);
    if (rightIcon != null) widgets.add(rightIcon);
    return widgets;
  }

  Widget _rightText() {
    if (rightButtonText == null) return null;
    Widget rightText = Clickable(
      onClick: () {
        if (this.onRightButtonClick != null) {
          this.onRightButtonClick();
        }
      },
      child: Container(
        padding: EdgeInsets.fromLTRB(Dimens.dimens16, 0, Dimens.dimens16, 0),
        child: Center(
          child: Text(
            rightButtonText,
            style: TextStyle(
              color: rightButtonTextColor == null
                  ? SQColor.white
                  : rightButtonTextColor,
              fontSize: Dimens.textSize28,
            ),
          ),
        ),
      ),
    );
    return rightText;
  }

  Widget _rightIcon() {
    if (rightButtonIcon == null) return null;
    Widget rightIcon = Clickable(
      onClick: () {
        if (this.onRightButtonIconClick != null) {
          this.onRightButtonIconClick();
        }
      },
      child: Container(
        padding: EdgeInsets.fromLTRB(Dimens.dimens16, 0, Dimens.dimens16, 0),
        child: Center(
          child: Icon(
            rightButtonIcon,
            color: rightButtonColor == null ? SQColor.white : rightButtonColor,
          ),
        ),
      ),
    );
    return rightIcon;
  }

  String getStr(Object message) {
    return message ??= "";
  }
}
